package com.example.satokenstarterdemo;

import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestApi {

    @GetMapping("/login")
    public String doLogin(LoginBean loginBean) throws JsonProcessingException {

        StpUtil.setLoginId(loginBean.getUid());
        String tokenValue = StpUtil.getTokenInfo().getTokenValue();
        loginBean.setToken(tokenValue);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(loginBean);
    }


    @GetMapping("/offline")
    public String execOffline(@RequestHeader("X-Access-Token") String headerToken){
        StpUtil.logoutByTokenValue(headerToken);
        return "下线成功!";
    }


    @GetMapping("/parse")
    public String parseTokenInfo(@RequestHeader("X-Access-Token") String headerToken){
        return "访问的用户:"+ StpUtil.getLoginIdByToken(headerToken);
    }

    @GetMapping("/test")
    public String testNotLogin(){
        return "测试免登录接口";
    }



}
